﻿/*
Copyright (c) 2013, Marc Tanti
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the www.marctanti.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MARIA.level.square;
using MARIA.level.direction;
using MARIA.level.situation;
using System.Windows.Forms;
using MARIA.level.robot;

namespace MARIA.program.condition
{
    [Serializable]
    /// <summary>
    /// Condition part of a statement.
    /// </summary>
    public class Condition
    {

        /// <summary>
        /// The direction to check.
        /// </summary>
        public Direction DirectionInQuestion
        {
            get
            {
                return directionInQuestion;
            }
            set
            {
                ChangeDirection(value);
            }
        }
        private Direction directionInQuestion;

        /// <summary>
        /// The square to compare to.
        /// </summary>
        public Square SquareInQuestion
        {
            get
            {
                return squareInQuestion;
            }
            set
            {
                ChangeSquare(value);
            }
        }
        private Square squareInQuestion;

        /// <summary>
        /// The test to use.
        /// </summary>
        public Test TestToUse
        {
            get
            {
                return testToUse;
            }
            set
            {
                ChangeTest(value);
            }
        }
        private Test testToUse;

        public delegate void ConditionChangedHandler(object sender, ConditionChangedEventArgs args);
        public event ConditionChangedHandler OnConditionChanged;

        /// <summary>
        /// Initialize a new instance of Condition using default parameters: UP == CLEAR
        /// </summary>
        public Condition()
        {
            this.directionInQuestion = Cache.Instance.Directions[0];
            this.squareInQuestion = Cache.Instance.Squares[0];
            this.testToUse = Cache.Instance.Tests[0];
        }

        public Condition(Direction direction, Test test, Square square)
            : this()
        {
            directionInQuestion = direction;
            testToUse = test;
            squareInQuestion = square;
        }

        public Condition(string description) : this()
        {
            foreach (Direction direction in Cache.Instance.Directions)
            {
                if (description.StartsWith(direction.TextualRepresentation))
                {
                    directionInQuestion = direction;
                    description = description.Substring(direction.TextualRepresentation.Length + 1);
                    break;
                }
            }

            foreach (Test test in Cache.Instance.Tests)
            {
                if (description.StartsWith(test.TextualRepresentation))
                {
                    testToUse = test;
                    description = description.Substring(test.TextualRepresentation.Length + 1);
                    break;
                }
            }

            foreach (Square square in Cache.Instance.Squares)
            {
                if (description.StartsWith(square.TextualRepresentation))
                {
                    squareInQuestion = square;
                    break;
                }
            }

            if (directionInQuestion == null || testToUse == null || squareInQuestion == null)
            {
                throw new ArgumentException();
            }
        }

        private void ChangeDirection(Direction newDirection)
        {
            Direction oldDirection = directionInQuestion;
            directionInQuestion = newDirection;
            OnConditionChanged(this, new ConditionChangedEventArgs(oldDirection, newDirection, squareInQuestion, squareInQuestion, testToUse, testToUse));
        }

        private void ChangeTest(Test newTest)
        {
            Test oldTest = testToUse;
            testToUse = newTest;
            OnConditionChanged(this, new ConditionChangedEventArgs(directionInQuestion, directionInQuestion, squareInQuestion, squareInQuestion, oldTest, newTest));
        }

        private void ChangeSquare(Square newSquare)
        {
            Square oldSquare = squareInQuestion;
            squareInQuestion = newSquare;
            OnConditionChanged(this, new ConditionChangedEventArgs(directionInQuestion, directionInQuestion, oldSquare, newSquare, testToUse, testToUse));
        }

        /// <summary>
        /// Test the condition on a situation.
        /// </summary>
        /// <param name="situation">The robot to check relative to.</param>
        /// <returns>Whether the test was true or not.</returns>
        public bool TestCondition(Robot robot)
        {
            return testToUse.MakeTest(robot, directionInQuestion, squareInQuestion);
        }

        /// <summary>
        /// Get a textual description of this condition.
        /// </summary>
        /// <returns>A textual description.</returns>
        public string TextualDescription()
        {
            return testToUse.GetTexualDescription(directionInQuestion, squareInQuestion);
        }

        public void SetEditPanel(Panel p)
        {
            //Direction
            ComboBox directionBox = new ComboBox();
            directionBox.DropDownStyle = ComboBoxStyle.DropDownList;
            foreach (Direction direction in Cache.Instance.Directions)
            {
                directionBox.Items.Add(direction.TextualRepresentation);
            }
            directionBox.SelectedIndex = Array.IndexOf(Cache.Instance.Directions, directionInQuestion);
            directionBox.SelectedIndexChanged += new EventHandler(
                delegate (object sender, EventArgs e)
                {
                    ChangeDirection(Cache.Instance.Directions[directionBox.SelectedIndex]);
                }
            );
            p.Controls.Add(directionBox);

            //Test
            ComboBox testBox = new ComboBox();
            testBox.DropDownStyle = ComboBoxStyle.DropDownList;
            foreach (Test test in Cache.Instance.Tests)
            {
                testBox.Items.Add(test.TextualRepresentation);
            }
            testBox.SelectedIndex = Array.IndexOf(Cache.Instance.Tests, testToUse);
            testBox.SelectedIndexChanged += new EventHandler(
                delegate(object sender, EventArgs e)
                {
                    ChangeTest(Cache.Instance.Tests[testBox.SelectedIndex]);
                }
            );
            p.Controls.Add(testBox);

            //Square
            ComboBox squareBox = new ComboBox();
            squareBox.DropDownStyle = ComboBoxStyle.DropDownList;
            foreach (Square square in Cache.Instance.Squares)
            {
                squareBox.Items.Add(square.TextualRepresentation);
            }
            squareBox.SelectedIndex = Array.IndexOf(Cache.Instance.Squares, squareInQuestion);
            squareBox.SelectedIndexChanged += new EventHandler(
                delegate(object sender, EventArgs e)
                {
                    ChangeSquare(Cache.Instance.Squares[squareBox.SelectedIndex]);
                }
            );
            p.Controls.Add(squareBox);
        }

    }
}
